<template>
  <van-icon
    :color="value ? '#3296fa' : '#777'"
    :name="value ? 'star' : 'star-o'"
    @click="onCollect"
  />
</template>

<script>
import { Toast } from 'vant'
import { addCollectArticle, deleteCollectArticle } from '@/api/article.js'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, Object, String],
      required: true
    }
  },
  data () {
    return {}
  },

  created () {},

  methods: {
    async onCollect () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '加载中...'
      })
      try {
        if (this.value) {
        // 已收藏
          await deleteCollectArticle(this.artId + '')
        } else {
        // 未收藏
          await addCollectArticle(this.artId + '')
        }
        this.$emit('input', !this.value)
        Toast.success('操作成功')
      } catch (err) {
        Toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
